﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Collections.Generic;

namespace Directory.Web
{
	public partial class Admin_Controls_CategoryTree : System.Web.UI.UserControl
	{
		protected void Page_Load(object sender, EventArgs e)
		{
			if (!IsPostBack)
			{
				IList<Directory.Model.CategoryInfo> categories = CategoryDataProxy.GetRootCategoriesForUser(HttpContext.Current.User.Identity.Name);

				if (categories == null)
				{
					return;
				}
				if (WebUtil.IsCurrentUserInRole(WebSettings.GetInstance().RootCategoryAdminRole))
				{
					TreeNode tn_base = new TreeNode("添加根" + Resources.Resource.ResourceManager.GetString("Category"), null, null, "~/Admin/category.aspx?action=addrootcategory", this.Target);
					tn_base.PopulateOnDemand = false;
					TreeView1.Nodes.Add(tn_base);

					TreeNode tn_recommend = new TreeNode("推荐" + Resources.Resource.ResourceManager.GetString("Category"), null, null, "~/Admin/RecommandCategory.aspx", this.Target);
					tn_recommend.PopulateOnDemand = false;
					TreeView1.Nodes.Add(tn_recommend);
				}

				TreeNode tn_Unaudited = new TreeNode("未审核" + Resources.Resource.Item, null, null, "~/Admin/unauditeditemview.aspx", this.Target);
				tn_Unaudited.PopulateOnDemand = false;
				TreeView1.Nodes.Add(tn_Unaudited);

				TreeNode tn_Search = new TreeNode("快速查找", null, null, "~/Admin/search.aspx", this.Target);
				tn_Search.PopulateOnDemand = false;
				TreeView1.Nodes.Add(tn_Search);

				foreach (Directory.Model.CategoryInfo cat in categories)
				{
					TreeNode node = new TreeNode(cat.CategoryName, cat.CategoryId.ToString(), null, this.NavigateUrl + QueryKey + "=" + cat.CategoryId.ToString(), this.Target);
					node.PopulateOnDemand = true;
					TreeView1.Nodes.Add(node);
				}

				ExpandCurrentNode(TreeView1);
			}
		}

		#region Properties

		private string _userName;
		public string UserName
		{
			get { return _userName; }
			set { _userName = value; }
		}

		private string _target;
		public string Target
		{
			get { return _target; }
			set { _target = value; }
		}

		private string _navigateUrl;
		public string NavigateUrl
		{
			get { return _navigateUrl; }
			set { _navigateUrl = value; }
		}

		private string _queryKey = "categoryId";
		public string QueryKey
		{
			get { return _queryKey; }
			set { _queryKey = value; }
		}

		public int RootsCount
		{
			get { return this.TreeView1.Nodes.Count; }
		}

		#endregion

		protected void TreeView1_TreeNodePopulate(object sender, TreeNodeEventArgs e)
		{
			if (e.Node.ChildNodes.Count == 0)
			{
				IList<Directory.Model.CategoryInfo> childs = CategoryDataProxy.GetChildsOfCategory(int.Parse(e.Node.Value));
				foreach (Directory.Model.CategoryInfo cat in childs)
				{
					TreeNode node = new TreeNode(cat.CategoryName, cat.CategoryId.ToString(), null, this.NavigateUrl + QueryKey + "=" + cat.CategoryId.ToString(), this.Target);
					node.PopulateOnDemand = true;
					e.Node.ChildNodes.Add(node);
				}
			}
		}

		private void ExpandCurrentNode(TreeView tv)
		{
			int categoryId;
			if (int.TryParse(Request.QueryString[QueryKey], out categoryId))
			{
				IList<Directory.Model.CategoryInfo> parentCateogries = CategoryDataProxy.GetRecursiveParentsOfCategory(categoryId);
				if (parentCateogries == null || parentCateogries.Count == 0)
				{
					return;
				}
				TreeNode baseNode = null;
				foreach (TreeNode node in tv.Nodes)
				{
					if (node.Value == parentCateogries[0].CategoryId.ToString())
					{
						baseNode = node;
						break;
					}
				}
				for (int i = 1; i < parentCateogries.Count && baseNode != null; i++)
				{
					baseNode = BoundChildNode(baseNode, parentCateogries[i]);
				}
				baseNode.Expanded = true;
				//baseNode.Selected = true;
			}
		}

		private TreeNode BoundChildNode(TreeNode node, Directory.Model.CategoryInfo cat)
		{
			node.PopulateOnDemand = true;
			node.Expand();
			foreach (TreeNode child in node.ChildNodes)
			{
				if (child.Value == cat.CategoryId.ToString())
				{
					return child;
				}
			}
			return null;
		}
	}
}
